package cn.zymall.user.service.impl;

import cn.zymall.common.model.Follow;
import cn.zymall.common.model.Merchant;
import cn.zymall.user.mapper.StoreAttentionMapper;
import cn.zymall.user.service.StoreAttentionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : th
 * @ClassName: StoreAttentionImpl
 * @Description : 店铺关注实现类
 * @date 2020年08月18日 14:32
 **/
@Service
public class StoreAttentionImpl implements StoreAttentionService {

    @Autowired
    private StoreAttentionMapper mapper;

    /**
     * @author：th
     * @Description：店铺关注查询
     * @Param：用户id
     * @return：List<Merchant>
     **/
    @Override
    public List<Merchant> follow(int id) {
        return mapper.follow(id);
    }


    /**
     * @author：th
     * @Description：关注表查询
     * @Param：用户ID
     * @return：List<Follow>
     **/
    @Override
    public Follow followId(int id,int mid) {
        return mapper.followId(id,mid);
    }

    /**
     * @author：th
     * @Description：店铺关注模糊查询
     * @Param：value 用户ID
     * @return：List<Follow>
     **/
    @Override
    public List<Merchant> fuzzyQuery(String value,int id) {
        List<Merchant> follow = follow(id);

        ArrayList<Merchant> obj = new ArrayList<>();

        for (Merchant merchant : follow) {
            if (merchant.getMerchantName().contains(value)){
                obj.add(merchant);
            }
        }
        return obj;
    }

    /**
     * @author：th
     * @Description：查询关注表有就修改，没有就添加
     * @Param：Follow
     * @return：boolean
     **/
    @Override
    public Boolean followSave(Follow follow) {

        int id = follow.getUserId();
        int mid = follow.getMerchantId();
        if (followId(id,mid) != null){
            return followUpdate(id,mid);
        }else{
            follow.setFollowTime(new Date());
            follow.setFollowStatus((byte) 1);
            return mapper.followSave(follow);
        }
    }

    /**
     * @author：th
     * @Description：关注表修改
     * @Param：用户ID，店铺ID
     * @return：Boolean
     **/
    @Override
    public Boolean followUpdate(int id,int mid) {
        return mapper.followUpdate(id,mid);
    }


    /**
     * @author：th
     * @Description：关注表删除
     * @Param：用户ID，店铺ID
     * @return：Boolean
     **/
    @Override
    public Boolean del(int id,int mid) {
        return mapper.del(id,mid);
    }



    /**
     * @author：th
     * @Description：关注表删除多个
     * @Param：用户ID ，店铺list
     * @return：Boolean
     **/
    @Override
    public Boolean removeList(int id,@Param("list") List list) {
        for (Object o : list) {
            System.out.println("---: "+o);
        }
        return mapper.removeList(id,list);
    }


}
